import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/config/address.dart';
import 'package:tiktokadult/config/config.dart';
import 'package:tiktokadult/hj_utils/global.dart';
import 'package:tiktokadult/hj_utils/utils.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/global_store/store.dart';

import '../../../hj_model/user/user_info_model.dart';
import '../../../hj_model/user/wallet_model_entity.dart';
import 'mine_share_record_view.dart';

//分享邀请
class MineSharePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MineSharePageState();
  }
}

class _MineSharePageState extends State<MineSharePage> {
  UserInfoModel? get meInfo => globalStore.getMe();

  WalletModelEntity? get wallet => globalStore.getWallet();
  ScrollController _controller = ScrollController();
  GlobalKey boundaryKey = GlobalKey();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("邀请分享"),
        actions: [
          InkWell(
            onTap: () {
              Get.to(() => MineShareRecordPage());
            },
            child: Text(
              "记录",
              style: TextStyle(
                color: Colors.white.withOpacity(.45),
                fontSize: 12,
              ),
            ),
          ),
          16.sizeBoxW,
        ],
      ),
      body: Container(
          padding: EdgeInsets.symmetric(horizontal: 16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                child: SingleChildScrollView(
                    controller: _controller,
                    child: RepaintBoundary(
                      key: boundaryKey,
                      child: Stack(
                        children: [
                          ClipRRect(
                            borderRadius: BorderRadius.circular(9),
                            child: Image.asset('invite_bg.webp'.mineImgPath),
                          ),
                          Positioned(
                            bottom: 30,
                            left: 0,
                            right: 0,
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Row(
                                  children: [
                                    Spacer(),
                                    Center(
                                      child: Container(
                                        padding: EdgeInsets.all(11.w),
                                        decoration: BoxDecoration(
                                          image: DecorationImage(
                                            image: AssetImage("code_bg.png".mineImgPath),
                                            fit: BoxFit.fill,
                                          ),
                                        ),
                                        child: Container(
                                          padding: EdgeInsets.all(6.w),
                                          color: Colors.white,
                                          child: QrImageView(
                                            data: globalStore.meInfo?.promoteURL ?? "",
                                            version: QrVersions.auto,
                                            size: 140,
                                            backgroundColor: Colors.white,
                                          ),
                                        ),
                                      ),
                                    ),
                                    Spacer(),
                                  ],
                                ),
                                6.sizeBoxH,
                                Text(
                                  '邀请码',
                                  style: TextStyle(
                                    color: Colors.white.withOpacity(.9),
                                    fontSize: 24,
                                    fontWeight: FontWeight.w500,
                                  ),
                                ),
                                6.sizeBoxH,
                                Text(
                                  '${globalStore.meInfo?.promotionCode ?? ""}',
                                  style: TextStyle(
                                    color: Colors.white.withOpacity(.9),
                                    fontSize: 36,
                                    fontWeight: FontWeight.w500,
                                  ),
                                ),
                                6.sizeBoxH,
                                Container(
                                  height: 30,
                                  padding: EdgeInsets.symmetric(horizontal: 18, vertical: 3),
                                  decoration: BoxDecoration(
                                    color: Colors.black.withOpacity(.3),
                                    borderRadius: BorderRadius.circular(30),
                                  ),
                                  child: Text(
                                    '${Config.APPNAME} 官网地址 ${Address.groundURl ?? ""}',
                                    style: TextStyle(
                                      color: Colors.white.withOpacity(.9),
                                      fontSize: 14,
                                      fontWeight: FontWeight.w400,
                                    ),
                                  ),
                                ),
                                4.sizeBoxH,
                                Text(
                                  '提示*苹果手机请用相机扫码/安卓手机推荐UC浏览器扫码',
                                  style: TextStyle(
                                    color: AppColors.actionRed,
                                    fontSize: 12,
                                    fontWeight: FontWeight.w400,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    )),
              ),
              Container(
                padding: EdgeInsets.symmetric(vertical: 12),
                child: Row(
                  children: [
                    GestureDetector(
                      onTap: () {
                        Clipboard.setData(ClipboardData(text: globalStore.meInfo?.promoteURL ?? ""));
                        showToast('复制成功');
                      },
                      child: Container(
                        width: 112,
                        height: 44,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(3),
                          color: Colors.white,
                        ),
                        child: Text(
                          "复制链接",
                          style: TextStyle(
                            fontSize: 16,
                            color: Color(0xff3D3D3D),
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ),
                    16.sizeBoxW,
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          _capturePngAndSaveToGallery();
                        },
                        child: Container(
                          height: 44,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(3),
                            color: AppColors.actionRed,
                          ),
                          child: Text(
                            "保存图片",
                            style: TextStyle(
                              fontSize: 16,
                              color: Colors.white,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          )),
    );
  }

  /// 将组件转化为图片数据
  Future<void> _capturePngAndSaveToGallery() async {
    RenderRepaintBoundary? boundary = boundaryKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;
    ui.Image? image = await boundary?.toImage(pixelRatio: 3.0);
    ByteData? byteData = await image?.toByteData(format: ui.ImageByteFormat.png);
    savePngDataToAblumn(byteData?.buffer.asUint8List());
  }
}
